<?php
/**
 * Created by PhpStorm.
 * User: win7
 * Date: 2018/5/10 0010
 * Time: 15:54
 */
namespace Wx\Controller;
use Think\Controller;
class StoreMessageController extends Controller
{
    public function Index(){
        $uid = session('u_id');
        $sid = session('s_id');
        $spellgroup = M("shop_spellgroup");
		
		$show_type=0;//默认显示全部
		if($_GET['show_type']>0)$show_type=$_GET['show_type'];
		$this->assign("show_type",$show_type);
		
        $spellgroup_info = $spellgroup->where("state <> 3 AND type = 1 AND u_id={$uid} AND s_id={$sid}")->order("id desc")->select();
        foreach ($spellgroup_info as $k => $v){
			$img=explode(',',$v['h_imgs']);
			$spellgroup_info[$k]['f_img']=$img[0];//第一张图片作为封面图
            switch ($spellgroup_info[$k]['state']){
                case 0:
                    $spellgroup_info[$k]['state'] = "审核中";$spellgroup_info_02[]=$spellgroup_info[$k];
                    break;
                case 1:
                    $spellgroup_info[$k]['state'] = "拼团中";$spellgroup_info_1[]=$spellgroup_info[$k];
                    break;
                case 2:
                    $spellgroup_info[$k]['state'] = "审核失败";$spellgroup_info_02[]=$spellgroup_info[$k];
                    break;
                case 4:
                    $spellgroup_info[$k]['state'] = "结束";$spellgroup_info_4[]=$spellgroup_info[$k];
                    break;
            }
        }
        $this->assign("spellgroup",$spellgroup_info);//全部
        $this->assign("spellgroup_02",$spellgroup_info_02);//审核状态
        $this->assign("spellgroup_1",$spellgroup_info_1);//拼团中
        $this->assign("spellgroup_4",$spellgroup_info_4);//已经结束
		
        $this->display("productmessage");
    }//end fun index 
    public function Editstore(){
        $category = M('category');
        $spellgroup = M('shop_spellgroup');
        $sid = session("s_id");
        $uid = session("u_id");
        $id = I("get.id")?I("get.id"):I("post.id");
        $op = I("get.op")?I("get.op"):I("post.op");
        //获取相关信息
        $list_category = $category->where('pid = 22 AND status = 1')->field('id,title')->select();//获取单位规格下的单位分类
        $this->assign('list_category',$list_category);
        if ($op){
            $post_group = I("post.");
            if ($op == 'add'){//上架拼团产品
                //添加拼团订单到数据库
                if($post_group){
                    $post_group['type'] = 1;
                    $post_group['u_id'] = $uid;
                    $post_group['s_id'] = $sid;
			//$post_group['wholesale']=$_POST['wholesale'];//农户报价
			        $post_group['server']=$_POST['server'];//拼团须知
                    $post_group['addtime'] = time();
                    $post_group['starttime'] = strtotime($post_group['starttime']);
                    $post_group['endtime'] = strtotime($post_group['endtime']);
					
					//处理多余的逗号		
					$data['h_imgs']=preg_replace('/(,)+/i',',',$_POST['h_imgs']);
					$data['h_imgs']=rtrim($data['h_imgs'],',');//去尾
					$post_group['h_imgs']=ltrim($data['h_imgs'],',');//去头
			
					$data['c_imgs']=preg_replace('/(,)+/i',',',$_POST['c_imgs']);
					$data['c_imgs']=rtrim($data['c_imgs'],',');//去尾
					$post_group['c_imgs']=ltrim($data['c_imgs'],',');//去头

                    //过滤字段
                    $lv = array('server');
//                    dump($post_group);die;
                    foreach($post_group as $k => $v){
                        if ($v == '' && !in_array($k,$lv)){
                            $this->error("请填写完相关数据");
                        }
                    }

                    $addgroup = $spellgroup->add($post_group);
                    if ($addgroup){
                        $this->redirect('StoreMessage/Index');
                    }else{
                        $this->error('添加失败');
                    }
                }
                $showtime = array(
                    'starttime' => '请输入开始时间',
                    'endtime' => '请输入结束时间'
                );
                $this->assign('spellgroup',$showtime);
                $this->assign('submit','立即发布');
            }else{
                $spellgroup_info = $spellgroup->where("id = {$id} AND type = 1 AND state <> 3")->find();//获取单位规格下的单位分类
                $spellgroup_info['starttime'] = date('Y-m-d H:i:s',$spellgroup_info['starttime']);
                $spellgroup_info['endtime'] = date('Y-m-d H:i:s',$spellgroup_info['endtime']);
				
				//图片分拆
				//首页
				$img=explode(',',$spellgroup_info['h_imgs']);
				$spellgroup_info['img1']=$img[0];
				$spellgroup_info['img2']=$img[1];
				//内容
				$img=explode(',',$spellgroup_info['c_imgs']);
				$spellgroup_info['img3']=$img[0];
				$spellgroup_info['img4']=$img[1];
				$spellgroup_info['img5']=$img[2];
				$spellgroup_info['img6']=$img[3];
				
                $this->assign('spellgroup',$spellgroup_info);
                $uid = $spellgroup_info['u_id'];
                $sid = $spellgroup_info['s_id'];
                if($op == 'update'){//更新拼团产品
                    if($post_group){
                        $post_group['type'] = 1;
                        $post_group['u_id'] = $uid;
                        $post_group['s_id'] = $sid;
                        $post_group['uptime'] = time();
                        $post_group['starttime'] = strtotime($post_group['starttime']);
                        $post_group['endtime'] = strtotime($post_group['endtime']);
			//$post_group['wholesale']=$_POST['wholesale'];//农户报价
			            $post_group['server']=$_POST['server'];//拼团须知
						//处理多余的逗号		
						$data['h_imgs']=preg_replace('/(,)+/i',',',$_POST['h_imgs']);
						$data['h_imgs']=rtrim($data['h_imgs'],',');//去尾
						$post_group['h_imgs']=ltrim($data['h_imgs'],',');//去头
				
						$data['c_imgs']=preg_replace('/(,)+/i',',',$_POST['c_imgs']);
						$data['c_imgs']=rtrim($data['c_imgs'],',');//去尾
						$post_group['c_imgs']=ltrim($data['c_imgs'],',');//去头
						
//                        var_dump($post_group);die;
                        //过滤字段
                        $lv = array('server','starttime','endtime');
                        foreach($post_group as $k => $v){
                            //数据检测
                            if ($v == '' && !in_array($k,$lv)){
//                                if($k == 'starttime' || $k == 'endtime'){
//                                    unset($post_group[$k]);
//                                }else{
									//dump($k);dump($v);die;
                                    $this->error("请填写完相关数据");
//                                }
                            }
                            //清除未改变数据
                            if ($spellgroup_info[$k] == $v){
                                unset($post_group[$k]);
                            }
                        }
                        $post_group['state'] = 0;
                        $savespellgroup = $spellgroup->where("id = {$id}")->save($post_group);
                        if ($savespellgroup){
                            $this->redirect('StoreMessage/Index');
                        }else{
                            $this->error('保存失败');
                        }
                    }
                    $this->assign('submit','立即保存');
                }//end if update
                if($op == 'shelves'){//重新上架
                    if($post_group) {
                        $post_group['type'] = 1;
                        //$post_group['state'] = 0;
                        $post_group['u_id'] = $uid;
                        $post_group['s_id'] = $sid;
                        $post_group['addtime'] = time();
                        $post_group['starttime'] = strtotime($post_group['starttime']);
                        $post_group['endtime'] = strtotime($post_group['endtime']);
			//$post_group['wholesale']=$_POST['wholesale'];//农户报价
			$post_group['server']=$_POST['server'];//拼团须知
						//处理多余的逗号		
						$data['h_imgs']=preg_replace('/(,)+/i',',',$_POST['h_imgs']);
						$data['h_imgs']=rtrim($data['h_imgs'],',');//去尾
						$post_group['h_imgs']=ltrim($data['h_imgs'],',');//去头
				
						$data['c_imgs']=preg_replace('/(,)+/i',',',$_POST['c_imgs']);
						$data['c_imgs']=rtrim($data['c_imgs'],',');//去尾
						$post_group['c_imgs']=ltrim($data['c_imgs'],',');//去头


                        $lv = array('server','starttime','endtime');

                        foreach ($post_group as $k => $v) {
                            //数据检测
                            if ($v == ''  && !in_array($k,$lv)) {
//                                if ($k == 'starttime' || $k == 'endtime') {
//                                    $post_group['starttime'] = strtotime($spellgroup_info['starttime']);
//                                    $post_group['endtime'] = strtotime($spellgroup_info['endtime']);
//                                } else {
                                    $this->error("请填写完相关数据");
//                                }
                            }
                        }//end foreach
                        $post_group['state'] = 0;
                        $newaddpellgroup = $spellgroup->add($post_group);
                        $deletepellgroup = $spellgroup->where("id = {$id}")->save(['state' => 3]);
                        if ($newaddpellgroup && $deletepellgroup) {
                            $this->redirect('StoreMessage/Index');
                        } else {
                            $this->error('上架失败');
                        }
                    }
                    $this->assign('submit','重新上架');
                }//end if shelves
            }//end if add
        }else{
            $this->error("无法进入");
        }//end if op
        $this->display('addproduct');
    }
    public function Deletestore(){
        $spellgroup = M("shop_spellgroup");
        $sgid = I("post.id");
        if ($sgid){
            $delete = $spellgroup->where("id = {$sgid}")->save(['state'=> 3]);
            if (!$delete){
                $this->error("删除失败");
            }
        }
    }//end fun delete
}//end class